package GUIStudy.Project.LoginGUI;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.HashMap;

public class MainBody extends JFrame implements ActionListener {
    Map<String,String> infor = new HashMap<>();
    JButton login = new JButton("登录");
    JButton registe = new JButton("注册");
    JPasswordField password = new JPasswordField();
    JTextField userName = new JTextField();
    JLabel showStatus = new JLabel("未进行操作");


    public MainBody() {
        frameBody();

        userNameBody();

        passwdBody();

        buttonBody();

        setShowStatus();

        this.setVisible(true);
    }

    private void frameBody() {
        this.setSize(500, 275);
        this.setTitle("login window");
        this.setAlwaysOnTop(true);
        this.setLayout(null);
        this.setDefaultCloseOperation(3);
    }

    private void userNameBody() {
        userName.setBounds(70, 30, 400, 30);
        this.add(userName);

        JLabel label = new JLabel("用户名");
        label.setBounds(30, 30, 40, 30);
        this.add(label);
    }

    private void passwdBody() {
        password.setBounds(70, 80, 400, 30);
        this.add(password);

        JLabel label = new JLabel("密码");
        label.setBounds(30, 80, 40, 30);
        this.add(label);
    }

    private void setShowStatus(){
        showStatus.setBounds(225, 160, 100, 50);
        this.add(showStatus);
    }

    private void buttonBody() {

        login.setBounds(75, 130, 100, 30);
        login.addActionListener(this);
        this.add(login);


        registe.setBounds(325, 130, 100, 30);
        registe.addActionListener(this);
        this.add(registe);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == login){
            if(password.getText().equals(infor.get(userName.getText())))
                showStatus.setText("登录成功!");
            else
                showStatus.setText("登录失败!");
        }else if(e.getSource() == registe){
            infor.put(userName.getText(), password.getText());
            showStatus.setText("注册成功");
        }
    }
}
